package com.zhx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;

/**
 * @program: web-framework-spring
 * @author: zhx
 * @create: 2021-06-11 13:35
 **/
@Repository //通过Spring注解定义一个DAO
public class LoginCountDao {
    private JdbcTemplate jdbcTemplate;
    private final static String DELETE_LOGIN_COUNT="delete from t_login_count";
    private final static String INSERT_LOGIN_COUNT="insert into t_login_count(user_id,login_count) select user_id,count(*) from t_login_log GROUP BY user_id";
    @Autowired //自动注入jdbctemplate的bean
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    /**
     * 进行插入数据,创建定时任务，每天凌晨2点执行
     * @return
     */
    @Scheduled(cron="0 0 2 ? * *")
    public void insertLoginCount() {
        jdbcTemplate.update(DELETE_LOGIN_COUNT);
        jdbcTemplate.update(INSERT_LOGIN_COUNT);
    }
}
